package org.black.phoneUtility.sms.reply.app.fragment;

import java.util.Calendar;

import org.black.phoneUtility.R;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;

public class SmsReplyReplyDateTimePeriodDatePickerDialogFragment extends
		DialogFragment {

	public static final String TAG = SmsReplyReplyDateTimePeriodDatePickerDialogFragment.class
			.getName();
	public static final String ARGUMENTS_IS_BEGIN = "is_begin";
	public static final String ARGUMENTS_YEAR = "year";
	public static final String ARGUMENTS_MONTH = "month";
	public static final String ARGUMENTS_DATE = "date";

	public interface SmsReplyReplyDateTimePeriodDatePickerInterface {
		public void onReplyDateTimePeriodDatePickerDialogPositiveClick(
				boolean isBegin, int year, int monthOfYear, int dayOfMonth);

		public void onReplyDateTimePeriodDatePickerDialogNegativeClick();
	}

	private SmsReplyReplyDateTimePeriodDatePickerInterface smsReplyReplyDateTimePeriodDatePickerInterface;

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		try {
			smsReplyReplyDateTimePeriodDatePickerInterface = (SmsReplyReplyDateTimePeriodDatePickerInterface) activity;
		} catch (ClassCastException e) {
			throw new ClassCastException(
					activity.toString()
							+ " must implement SmsReplyReplyDateTimePeriodDatePickerInterface");
		}
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {
		super.onCreateDialog(savedInstanceState);
		Bundle bundle = getArguments();
		final boolean isBegin = bundle.getBoolean(ARGUMENTS_IS_BEGIN);
		int year = bundle.getInt(ARGUMENTS_YEAR);
		int month = bundle.getInt(ARGUMENTS_MONTH);
		int date = bundle.getInt(ARGUMENTS_DATE);
		AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

		LayoutInflater inflater = getActivity().getLayoutInflater();
		View view = inflater.inflate(
				R.layout.sms_reply_reply_date_time_period_date_picker_dialog,
				null);
		final DatePicker smsReplyDateTimePeriodDatePickerDatePicker = (DatePicker) view
				.findViewById(R.id.smsReplyDateTimePeriodDatePickerDatePicker);
		smsReplyDateTimePeriodDatePickerDatePicker
				.init(year, month, date, null);
		Button smsReplyDateTimePeriodDatePickerTodayButton = (Button) view
				.findViewById(R.id.smsReplyDateTimePeriodDatePickerTodayButton);
		smsReplyDateTimePeriodDatePickerTodayButton
				.setOnClickListener(new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						Calendar calendar = Calendar.getInstance();
						int year = calendar.get(Calendar.YEAR);
						int month = calendar.get(Calendar.MONTH);
						int date = calendar.get(Calendar.DATE);
						smsReplyDateTimePeriodDatePickerDatePicker.init(year,
								month, date, null);
					}
				});

		builder.setView(view)
				.setPositiveButton(android.R.string.ok,
						new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								smsReplyReplyDateTimePeriodDatePickerInterface
										.onReplyDateTimePeriodDatePickerDialogPositiveClick(
												isBegin,
												smsReplyDateTimePeriodDatePickerDatePicker
														.getYear(),
												smsReplyDateTimePeriodDatePickerDatePicker
														.getMonth(),
												smsReplyDateTimePeriodDatePickerDatePicker
														.getDayOfMonth());

							}
						})
				.setNegativeButton(android.R.string.cancel,
						new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								// TODO Auto-generated method stub

							}
						});
		return builder.create();
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
	}

}
